package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ROB_REPORT, desc = "战报")
public class BeautyEscortReportCmd extends AbstractCommand {

    int type;
    long dartId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortReportReqMsg reqMsg = BeautyEscortProto.BeautyEscortReportReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        if(reqMsg.hasDartId()){
            dartId = reqMsg.getDartId();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        boolean groupOpen = player.getModule(BeautyEscortModule.class).systemOpen();
        if(groupOpen){
            BeautyEscortProto.CrossBeautyEscortReportReqMsg.Builder reqMsg = BeautyEscortProto.CrossBeautyEscortReportReqMsg.newBuilder();
            reqMsg.setType(type);
            reqMsg.setDartId(dartId);
            if(type == 1){
                UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId());
                reqMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
            }
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
            if(memberMap != null){
                for (Map.Entry<Long, CrossUnionMember> entry : memberMap.entrySet()) {
                    reqMsg.addMemberIdList(entry.getKey());
                }
            }
            player.sendPacket(Protocol.C_BEAUTY_ESCORT_ROB_REPORT, reqMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortReportRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortReportRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setType(type);
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_ROB_REPORT, respMsg);
        }
    }
}
